package com.cb.controller.admin.system;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.system.Company;
import com.cb.domain.service.system.CompanyManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class CompanyFormController extends BaseFormController {
	
	@Autowired
	private CompanyManager companyManager=null;
	
	public CompanyFormController() {
		 setCommandName("company");
	     setCommandClass(Company.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Company company=(Company)command;
		String msg=null;
		//validate the logic business data here [TODO]
		if(company.getCompanyId()==null){
			msg=getText("company.added",company.getCompanyId());
		}else{
			msg=getText("company.updated",company.getCompanyId());
		}
		
		companyManager.save(company);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Company company=null;
		String companyId=request.getParameter("companyId");
		if(StringUtils.isNotEmpty(companyId)){
			company=companyManager.get(new Long(companyId));
		}else{
			company=new Company();
		}
		return company;
	}
	
}
